Welcome Guest | Sign in | Register
Data Files - C Programming Interview Questions and Answers | LucentBlackBoard | LucentBlackBoard.com

Home > Technical Interviews > Computer Science & Engineering > C Programming > Data Files Questions and Answers

11. How do you determine a file's attributes?

The file attributes are stored in the find_t.attrib structure member. This structure member is a single character, and each file attribute is represented by a single bit. Here is a list of the valid DOS file attributes:
Value  Description        Constant
0x00 - Normal -             (none)
0x01 - Read Only -       FA_RDONLY
0x02 - Hidden File -      FA_HIDDEN
0x04 - System File -     FA_SYSTEM
0x08 - Volume Label -   FA_LABEL
0x10 - Subdirectory -     FA_DIREC
0x20 - Archive File -       FA_ARCHIVE
To determine the file's attributes, you check which bits are turned on and map them corresponding to the preceding table. For example, a read-only hidden system file will have the first, second, and third bits turned on. A "normal" file will have none of the bits turned on. To determine whether a particular bit is turned on, you do a bit-wise AND with the bit's constant representation.
The following program uses this technique to print a file's attributes:
#include
#include
#include
#include
#include
#include
typedef struct find_t FILE_BLOCK;
void main(void);
void main(void)
{
FILE_BLOCK f_block; /* Define the find_t structure variable */
int ret_code; /* Define a variable to store the return codes */
printf("\nDirectory listing of all files in this directory:\n\n");
/* Use the "*.*" file mask and the 0xFF attribute mask to list
all files in the directory, including system files, hidden
files, and subdirectory names. */
ret_code = _dos_findfirst("*.*", 0xFF, &f_block);
/* The _dos_findfirst() function returns a 0 when
it is successful and has found a valid filename
in the directory. */
while (ret_code == 0)
{
/* Print the file's name */
printf("%-12s ", f_block.name);
/* Print the read-only attribute */
printf("%s ", (f_block.attrib & FA_RDONLY) ? "R" : ".");
/* Print the hidden attribute */
printf("%s ", (f_block.attrib & FA_HIDDEN) ? "H" : ".");
/* Print the system attribute */
printf("%s ", (f_block.attrib & FA_SYSTEM) ? "S" : ".");
/* Print the directory attribute */
printf("%s ", (f_block.attrib & FA_DIREC) ? "D" : ".");
/* Print the archive attribute */
printf("%s\n", (f_block.attrib & FA_ARCH) ? "A" : ".");
/* Use the _dos_findnext() function to look
for the next file in the directory. */
ret_code = _dos_findnext(&f_block);
}
printf("\nEnd of directory listing.\n");
}

12. How do you view the PATH?

Your C compiler library contains a function called getenv() that can retrieve any specified environment variable. It has one argument, which is a pointer to a string containing the environment variable you want to retrieve. It returns a pointer to the desired environment string on successful completion. If the function cannot find your environment variable, it returns NULL.
The following example program shows how to obtain the PATH environment variable and print it on-screen:
#include
#include
void main(void);
void main(void)
{
char* env_string;
env_string = getenv("PATH");
if (env_string == (char*) NULL)
printf("\nYou have no PATH!\n");
else
printf("\nYour PATH is: %s\n", env_string);
}

13. How can I open a file so that other programs can update it at the same time?

Your C compiler library contains a low-level file function called sopen() that can be used to open a file in shared mode. Beginning with DOS 3.0, files could be opened in shared mode by loading a special program namedSHARE.EXE. Shared mode, as the name implies, allows a file to be shared with other programs as well as your own. Using this function, you can allow other programs that are running to update the same file you are updating.
The sopen() function takes four parameters: a pointer to the filename you want to open, the operational mode you want to open the file in, the file sharing mode to use, and, if you are creating a file, the mode to create the file in. The second parameter of the sopen() function, usually referred to as the "operation flag" parameter, can have the following values assigned to it:
Constant          Description
O_APPEND - Appends all writes to the end of the file
O_BINARY - Opens the file in binary (untranslated) mode
O_CREAT - If the file does not exist, it is created
O_EXCL - If the O_CREAT flag is used and the file exists, returns an error
O_RDONLY - Opens the file in read-only mode
O_RDWR - Opens the file for reading and writing
O_TEXT - Opens the file in text (translated) mode
O_TRUNC - Opens an existing file and writes over its contents
O_WRONLY - Opens the file in write-only mode
The third parameter of the sopen() function, usually referred to as the "sharing flag," can have the following values assigned to it:
Constant          Description
SH_COMPAT - No other program can access the file
SH_DENYRW - No other program can read from or write to the file
SH_DENYWR - No other program can write to the file
SH_DENYRD - No other program can read from the file
SH_DENYNO - Any program can read from or write to the file
If the sopen() function is successful, it returns a non-negative number that is the file's handle. If an error occurs, -1 is returned, and the global variable errno is set to one of the following values:
Constant      Description
ENOENT - File or path not found
EMFILE - No more file handles are available
EACCES - Permission denied to access file
EINVACC - Invalid access code
The following example shows how to open a file in shared mode:
#include
#include
#include
#include
#include
void main(void);
void main(void)
{
int file_handle;
/* Note that sopen() is not ANSI compliant */
file_handle = sopen("C:\\DATA\\TEST.DAT", O_RDWR, SH_DENYNO);
close(file_handle);
}
Whenever you are sharing a file's contents with other programs, you should be sure to use the standard C library function named locking() to lock a portion of your file when you are updating it.

14. How can I make sure that my program is the only one accessing a file?

By using the sopen() function, you can open a file in shared mode and explicitly deny reading and writing permissions to any other program but yours. This task is accomplished by using the SH_DENYWR shared flag to denote that your program is going to deny any writing or reading attempts by other programs. For example, the following snippet of code shows a file being opened in shared mode, denying access to all other files:
/* Note that the sopen() function is not ANSI compliant... */
fileHandle = sopen("C:\\DATA\\SETUP.DAT", O_RDWR, SH_DENYWR);
By issuing this statement, all other programs are denied access to the SETUP.DAT file. If another program were to try to open SETUP.DAT for reading or writing, it would receive an EACCES error code, denoting that access is denied to the file.




Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.